package top.superflow.flow.config.parse.impl

import top.superflow.core.utils.{IdentifiableUtils, PojoUtils}
import top.superflow.exception.{Exceptable, RequestUnsupportedException}
import top.superflow.flow.config.parse.Parser
import top.superflow.flow.mapper.MapperConfig
import top.superflow.flow.runtime.mapper.RuntimeMapper

class MapperConfigParser extends Parser[RuntimeMapper] with Exceptable{
  
  def parse(original : Any) : RuntimeMapper = {
    original match {
      case mapperConfig: MapperConfig => {
        val mapperAny = PojoUtils.createInstance(mapperConfig.getMapper)
        if(!mapperAny.isInstanceOf[RuntimeMapper]){
          throwRequestUnsupportedException("The configured mapper type {} is not sub type of {}", mapperConfig.getMapper, classOf[RuntimeMapper])
        }

        val runtimeMapper = mapperAny.asInstanceOf[RuntimeMapper]
        IdentifiableUtils.copy(mapperConfig, runtimeMapper)

        runtimeMapper
      }
      case _ => {
        throw new RequestUnsupportedException("Only type FlowConfig is supported, please sure pass the correct type.")
      }
    }
  }
}